<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //创建函数
      //1.普通创建 (常用)
      function fn(n) {
        console.log(n);
      }
      fn(10);

      //2.使用变量初始化函数 (常用)
      var fn1 = function (n) {
        console.log(n);
      };
      fn1(100);

      //3.new 创建
      var fn2 = new Function();
      fn2 = function (n) {
        console.log(n);
      };
      fn2(1000);

      //函数传参
      //1.不传参（哑参）
      function show() {
        console.info("王大头小朋友你好呀！");
      }
      show();
      //2.传参(参数可以是变量，对象，数组，表达式，函数等)
      var person = function (obj) {
        return obj.name + "," + obj.sex;
      };
      var a = {
        name: "张三",
        sex: "男",
        age: 18,
      };
      console.log(person(a));

      //info给默认值 传递info值将覆盖 未传值是将使用默认值
      function person1(obj, info = "是一位好学生！") {
        return obj.name + "," + obj.sex + "," + info;
      }
      console.log(person1(a));
      console.log(person1(a, "学习很好"));

      function box(sumFunction, num) {//形参中含有函数
        return sumFunction(num);//返回一个函数 sum(10); 
      }
      function sum(num) {
        return num + 10;//20
      }
      var result = box(sum, 10); //传递函数到另一个函数里
      console.log(result);
    </script>
  </body>
</html>
